package com.hkfn.rfb.common.http;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.http.Json.ParseJsonData;
import com.hkfn.rfb.common.utils.StringUtils;
import com.hkfn.rfb.common.utils.ToastUtils;
import com.hkfn.rfb.common.utils.Utils;
import com.xh.common.thread.AsyncExecutor;

import java.io.File;
import java.util.HashMap;

public class HttpHelper {
    private BaseActivity activity;

    private String url;

    private int what;

    private Handler handler;

    private boolean isPost = false;

    private HashMap<String, String> params;

    private File file;

    private ParseJsonData parseJsonData;

    public HttpHelper(BaseActivity activity, String url, int what, Handler handler) {
        this.activity = activity;
        this.url = url;
        this.what = what;
        this.handler = handler;
    }

    public HttpHelper(BaseActivity activity, String url, int what, Handler handler, boolean isPost, HashMap<String, String> params) {
        this.activity = activity;
        this.url = url;
        this.what = what;
        this.handler = handler;
        this.isPost = isPost;
        this.params = params;
    }

    public HttpHelper(BaseActivity activity, String url, int what, Handler handler, File file) {
        this.activity = activity;
        this.url = url;
        this.what = what;
        this.handler = handler;
        this.file = file;
    }

    public ParseJsonData getParseJsonData() {
        return parseJsonData;
    }

    public void setParseJsonData(ParseJsonData parseJsonData) {
        this.parseJsonData = parseJsonData;
    }

    public void getHttpRequest(){
        getHttpRequest(false);
    }

    public void getHttpRequest(final boolean needJson){
        if (StringUtils.isEmpty(url)){
            ToastUtils.showToast(activity, "请求url不能为空");
            return;
        }
        if (!Utils.hasNetwork(activity)){
            ToastUtils.showToast(activity, activity.getResources().getString(R.string.no_net));
            return;
        }
        Log.e("url", url);
        AsyncExecutor.execute(new Runnable() {
            @Override
            public void run() {
                // 请求数据
                requestUrlData(needJson);
            }
        });

    }

    public void getHttpFileRequest(){
        if (StringUtils.isEmpty(url)){
            ToastUtils.showToast(activity, "请求url不能为空");
            return;
        }
        if (!Utils.hasNetwork(activity)){
            ToastUtils.showToast(activity, activity.getResources().getString(R.string.no_net));
            return;
        }
        Log.e("url", url);
        AsyncExecutor.execute(new Runnable() {
            @Override
            public void run() {
                // 请求数据
                requestFileUrlData();
            }
        });
    }


    private void requestUrlData(boolean needJson) {
        String result;
        if (needJson){
            result = HttpConnection.postRequestByJsonWithOkHttp(activity, url, params);
        }else {
            if (isPost) {
                result = HttpConnection.postRequestWithOkHttp(activity, url, params);
            } else {
                result = HttpConnection.getRequestWithOkHttp(activity, url);
            }
        }
        if (!StringUtils.isEmpty(result)) {
            Log.e("url", result);
            // 处理返回结果
            parseJsonData.parseResponseWithGSON(result, activity, handler, what);
        }

        if (StringUtils.isEmpty(result)) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast(activity, "服务器异常，请稍后重试");
                }
            });
            return;
        }

    }

    private void requestFileUrlData() {
        String result = HttpConnection.postRequestByFileWithOkHttp(activity, url, file);
        if (!StringUtils.isEmpty(result)){
            Log.e("url", result);
            // 处理返回结果
            parseJsonData.parseResponseWithGSON(result, activity, handler, what);
        }else {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast(activity, activity.getResources().getString(R.string.request_error));
                }
            });
        }


        if (StringUtils.isEmpty(result)) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast(activity, "服务器异常，请稍后重试");
                }
            });
            return;
        }

    }


    /**
     * 网络连接异常时的动作
     * @param errorCode
     * @param errorMsg
     */
    private void urlConnectionExceptionAction(int errorCode, String errorMsg) {
        handler.removeMessages(what);
        Message message = handler.obtainMessage();
        message.arg1 = errorCode;
        message.what = what;
        Bundle bundle = new Bundle();
        bundle.putBoolean(ParseJsonData.REQUEST_OK, false);
        bundle.putString(ParseJsonData.ERROR_MSG, errorMsg);
        message.setData(bundle);
        handler.sendMessage(message);
    }


}
